import entity.Key;
import entity.student;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@WebServlet(urlPatterns = {"/login/dgut"})
public class login extends HttpServlet {

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String result2;
        String token=req.getParameter("token");

        List<NameValuePair> formparams = new ArrayList<>();
        formparams.add(new BasicNameValuePair("token", token));
        formparams.add(new BasicNameValuePair("appid", "javaee"));
        formparams.add(new BasicNameValuePair("appsecret", "b3b52e43ccfd"));
        formparams.add(new BasicNameValuePair("userip", "10.0.2.2"));
        String url="https://cas.dgut.edu.cn/ssoapi/v2/checkToken";
        HttpPost httppost = new HttpPost(url);
        UrlEncodedFormEntity uefEntity;

        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(uefEntity);
            System.out.println("executing request " + httppost.getURI());

            try (CloseableHttpResponse response = httpclient.execute(httppost)) {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    String result = EntityUtils.toString(entity, "UTF-8");
                    Jsonb jsonb = JsonbBuilder.create();
                    Key key = jsonb.fromJson(result, Key.class);
                    String access_token1=key.getAccess_token();
                    String oppenid1=key.getOppenid();

                    if(access_token1!=null){
                        List<NameValuePair> formparams2 = new ArrayList<>();
                        formparams2.add(new BasicNameValuePair("access_token", access_token1));
                        formparams2.add(new BasicNameValuePair("openid",oppenid1));
                        String url2="https://cas.dgut.edu.cn/oauth/getUserInfo";
                        HttpPost httppost2 = new HttpPost(url2);
                        UrlEncodedFormEntity uefEntity2;

                        try (CloseableHttpClient httpclient2 = HttpClients.createDefault()) {
                            uefEntity2 = new UrlEncodedFormEntity(formparams2, "UTF-8");
                            httppost2.setEntity(uefEntity2);

                            try (CloseableHttpResponse response2 = httpclient2.execute(httppost2)) {
                                HttpEntity entity2 = response2.getEntity();
                                if (entity != null) {
                                    result2 = EntityUtils.toString(entity2, "UTF-8");
                                    Jsonb jsonb2 = JsonbBuilder.create();
                                    student stu = jsonb2.fromJson(result2, student.class);
                                    HttpSession session = req.getSession(true);
                                    Map<String, String> info = new HashMap<String, String>();
                                    info.put("username", stu.getUsername());
                                    info.put("name", stu.getName());
                                    info.put("faculty_title", stu.getFaculty_title());
                                    info.put("group", stu.getGroup());
                                    info.put("faculty",stu.getFaculty());
                                    info.put("wx_openid",stu.getWx_openid());
                                    info.put("openid",stu.getOpenid());
                                    session.setAttribute("Login", info);
                                    resp.sendRedirect("/index.jsp");
                                }
                            }
                        }catch (IOException e) {
                            e.printStackTrace();
                        }

                    }

                }



            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}

